package com.dianxin.smartpark.service.common.impl;

import java.util.Date;
import java.util.List;

import cn.dev33.satoken.stp.StpUtil;
import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.Convert;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.acl.api.dto.SsoUserDTO;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkServiceAddres;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkServiceAddresExample;
import com.dianxin.smartpark.dao.mapper.common.SmartParkServiceAddresMapper;
import com.dianxin.smartpark.dao.qo.common.SmartParkServiceAddresQO;
import com.dianxin.smartpark.service.common.SmartParkServiceAddresService;
import com.dianxin.smartpark.service.vo.common.SmartParkServiceAddresVO;
import com.dianxin.sso.client.context.UserHolderContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 服务地址管理业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-28
 */
@Service
public class SmartParkServiceAddresServiceImpl implements SmartParkServiceAddresService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkServiceAddresServiceImpl.class);
    @Resource
    private SmartParkServiceAddresMapper smartParkServiceAddresMapper;

    /**
     * 查询服务地址管理
     * 
     * @param qo 服务地址管理ID
     * @return 服务地址管理
     */
    @Override
    public CommonResponseBody<SmartParkServiceAddresVO> selectSmartParkServiceAddres(SmartParkServiceAddresQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkServiceAddresExample smartParkServiceAddresExample = new SmartParkServiceAddresExample();
        SmartParkServiceAddresExample.Criteria criteria = smartParkServiceAddresExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkServiceAddres> passRecords = smartParkServiceAddresMapper.selectByExample(smartParkServiceAddresExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkServiceAddresVO.class);
    }

    /**
     * 查询服务地址管理列表
     * 
     * @param qo 服务地址管理
     * @return 服务地址管理
     */
    @Override
    public CommonResponseBody<List<SmartParkServiceAddresVO>> selectSmartParkServiceAddresList(SmartParkServiceAddresQO qo)
    {
        SmartParkServiceAddresExample smartParkServiceAddresExample = new SmartParkServiceAddresExample();
        SmartParkServiceAddresExample.Criteria criteria = smartParkServiceAddresExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        SsoUserDTO ssoUserDTO = UserHolderContext.getUser();
        //String loginIdAsString = StpUtil.getLoginIdAsString();
        //criteria.andCreatorEqualTo(loginIdAsString);
        criteria.andCreatorEqualTo(String.valueOf(ssoUserDTO.getUserId()));
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkServiceAddresExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkServiceAddresExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkServiceAddres> passRecords = smartParkServiceAddresMapper.selectByExample(smartParkServiceAddresExample);
        long count = smartParkServiceAddresMapper.countByExample(smartParkServiceAddresExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkServiceAddresVO.class);
    }

    /**
     * 新增服务地址管理
     * 
     * @param vo 服务地址管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkServiceAddres(SmartParkServiceAddresVO vo)
    {
        // 验证 todo
        if(vo.getStatus()==1) {
            SmartParkServiceAddres smartParkServiceAddres = new SmartParkServiceAddres();
            smartParkServiceAddres.setStatus(0);
            SmartParkServiceAddresExample smartParkServiceAddresExample = new SmartParkServiceAddresExample();
            SmartParkServiceAddresExample.Criteria criteria = smartParkServiceAddresExample.createCriteria();
            SsoUserDTO ssoUserDTO = UserHolderContext.getUser();
            criteria.andCreatorEqualTo(Convert.toStr(ssoUserDTO.getUserId()));
            smartParkServiceAddresMapper.updateByExampleSelective(smartParkServiceAddres, smartParkServiceAddresExample);
        }
        SmartParkServiceAddres smartParkServiceAddres = new SmartParkServiceAddres();
        try {
            BeanUtil.insertConversion(vo, smartParkServiceAddres);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkServiceAddres.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkServiceAddresMapper.insertSelective(smartParkServiceAddres));
    }

    /**
     * 修改服务地址管理
     * 
     * @param vo 服务地址管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkServiceAddres(SmartParkServiceAddresVO vo)
    {
        // 验证 todo
        if(vo.getStatus()==1) {
            SmartParkServiceAddres smartParkServiceAddres = new SmartParkServiceAddres();
            smartParkServiceAddres.setStatus(0);
            SmartParkServiceAddresExample smartParkServiceAddresExample = new SmartParkServiceAddresExample();
            SmartParkServiceAddresExample.Criteria criteria = smartParkServiceAddresExample.createCriteria();
            SsoUserDTO ssoUserDTO = UserHolderContext.getUser();
            criteria.andCreatorEqualTo(Convert.toStr(ssoUserDTO.getUserId()));
            smartParkServiceAddresMapper.updateByExampleSelective(smartParkServiceAddres, smartParkServiceAddresExample);
        }
        SmartParkServiceAddres smartParkServiceAddres = new SmartParkServiceAddres();
        try {
            BeanUtil.insertConversion(vo, smartParkServiceAddres);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkServiceAddres.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkServiceAddresMapper.updateByPrimaryKeySelective(smartParkServiceAddres));
    }

    /**
     * 删除服务地址管理信息
     * 
     * @param vo 服务地址管理
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkServiceAddres(SmartParkServiceAddresVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkServiceAddres smartParkServiceAddres = new SmartParkServiceAddres();
        try {
            BeanUtil.insertConversion(vo, smartParkServiceAddres);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkServiceAddres.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkServiceAddresMapper.updateByPrimaryKeySelective(smartParkServiceAddres));
    }
}
